package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjTrainDetail;
import com.pmcc.cj.repository.CjTrainDetailRepository;
import com.pmcc.cj.repository.CjTrainRepository;
import com.pmcc.core.entity.Employee;
import com.pmcc.core.repository.EmployeeRepository;
import com.pmcc.utils.OnlineUser;
import com.pmcc.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by tomas on 2020/9/12.
 */
@Transactional
@Service
public class CjTrainDetailService {
    @Autowired
    CjTrainRepository cjTrainRepository;
    @Autowired
    CjTrainDetailRepository cjTrainDetailRepository;
    @Autowired
    EmployeeRepository employeeRepository;

    public ResultBean addTrainDetail(CjTrainDetail model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Employee employee = employeeRepository.findOne(OnlineUser.getEmployeeId());
            model.setEmployeeId(employee.getId());
            model.setName(employee.getUserName());
            model.setCellphone(employee.getCellphone());
            model.setCreateTime(new Date());
            cjTrainDetailRepository.saveAndFlush(model);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean deleteTrainDetail(String id){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            cjTrainDetailRepository.delete(id);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean queryTrainDetail(String trainId){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            resultBean.setRows(cjTrainDetailRepository.findByTrainId(trainId));
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean queryTrainDetailEmployee(){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            resultBean.setRows(cjTrainRepository.queryTrainDetailEmployee(OnlineUser.getEmployeeId()));
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
